package com.yn.club.auth.domain.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yn.club.common.enums.IsDeletedFlagEnum;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yn.club.auth.domain.entity.AuthRolePermissionDO;
import com.yn.club.auth.infra.basic.entity.AuthRolePermission;
import com.yn.club.auth.domain.service.AuthRolePermissionDomainService;
import com.yn.club.auth.infra.basic.service.AuthRolePermissionService;
import com.yn.club.auth.domain.convert.AuthRolePermissionDomainConverter;


/**
 * 角色权限关联表(AuthRolePermission)表服务接口实现类
 *
 * @author YN
 * @since 2025-01-22 10:07:43
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

  private final AuthRolePermissionService authRolePermissionService;
  private final AuthRolePermissionDomainConverter authRolePermissionDomainConverter;


  /**
   * 分页
   */
  @Override
  public PageResp<AuthRolePermissionDO> page(PageReq pageReq, AuthRolePermissionDO query){
    QueryWrapper<AuthRolePermission> querywrapper = new QueryWrapper<AuthRolePermission>()
        .eq(query.getId() != null, "id", query.getId())
        .orderByDesc("id");
        
    Page<AuthRolePermission> page = authRolePermissionService.page(
      new Page<>(pageReq.getPageIndex(),pageReq.getPageSize()),querywrapper);
    List<AuthRolePermission> list = page.getRecords();  
    return PageResp.of(page,authRolePermissionDomainConverter.convertEntityToDo(list));
  }

  /**
   * 新增数据
   */
  @Override
  public Boolean insert(AuthRolePermissionDO authRolePermissionDO){
    List<AuthRolePermission> rolePermissionList = new LinkedList<>();
    Long roleId = authRolePermissionDO.getRoleId();
    authRolePermissionDO.getPermissionIdList().forEach(permissionId -> {
      AuthRolePermission authRolePermission = new AuthRolePermission();
      authRolePermission.setRoleId(roleId);
      authRolePermission.setPermissionId(permissionId);
      authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
      rolePermissionList.add(authRolePermission);
    });
    boolean saveBatch = authRolePermissionService.saveBatch(rolePermissionList);
    return saveBatch;
  };


  /**
   * 更新数据
   */
  @Override
  public Boolean updateById(AuthRolePermissionDO obj){
    AuthRolePermission authRolePermission = authRolePermissionDomainConverter.convertDoToEntity(obj);
    return authRolePermissionService.updateById(authRolePermission);
  };

  /**
   * 批量删除
   */
  @Override
  public Boolean delete(List<Integer> ids){
    return authRolePermissionService.removeByIds(ids);
  };

  /**
   * 根据id查找
   */
  @Override
  public AuthRolePermissionDO getById(Serializable id){
    AuthRolePermission authRolePermission = authRolePermissionService.getById(id);
    return  authRolePermissionDomainConverter.convertEntityToDo(authRolePermission);
  };


}

